mui.init({
	beforeback: function(){
		return true;
	}
});

var old_back = mui.back;
mui.back = function(){
  var btn = ["确定","取消"];
  mui.confirm('确认退出登录？','提示',btn,function(e){
    if(e.index==0){
    	    MuiEventMgr.loginout();
	    	var indexPage = plus.webview.getLaunchWebview();
	    	plus.storage.setItem(KeyCfg.currClickTabId,"defaultTab");
	    indexPage.show();
	    mui.fire(indexPage,'refresh',{});
	    plus.webview.currentWebview().close();
//		mui.openWindow("../../index.html");
    }
  });
}


var pars = null;
var isInitSend = false;
/**
 * 刷新事件 
 * loginPageId
 */
window.addEventListener('reload',function(event){
	var userId = event.detail.userId;
	var phone = event.detail.phone;
	startSendSms(userId,phone);
});


/**
 * 开始发送手机短信 
 * @param {Object} userId
 * @param {Object} phone
 */
function startSendSms(userId, phone){
	if(!phone){
		mui.alert("要发送的手机号码不能为空!");
		return;
	}
	$("#phone").text(phone);
	pars = {userId:userId,phone:phone,save:"ysjsgzq",smstype:"validPhone"};
	sendSms(pars);
}

mui.plusReady(function() {
	var p_phone = plus.webview.currentWebview().phone;
	var p_userId = plus.storage.getItem(KeyCfg.userId);
	if(p_phone) startSendSms(p_userId, p_phone);
	var btn_finish = CmwMcore.$('btn_finish');
	MeventMgr.onTouchstart(btn_finish,function(event,self){
		var vcode = $("#vcode").val();
		if(!vcode){
			plus.nativeUI.toast("请输入您手机收到的验证码");
			return;
		}
		doFinish();
		return false;
	});
	
	
	/*------- 登录事件绑定 --------*/
	var btn_backLogin = CmwMcore.$('backLogin');
	MeventMgr.onTouchstart(btn_backLogin,function(event,self){
		forward();
		return false;
	});
	
	function doFinish(){
		var phonenum = pars.phone;
		var validcode = $("#vcode").val();
		var userId = plus.storage.getItem(KeyCfg.userId);
		var data = {userId:userId,phonenum:phonenum,validcode:validcode};
		var url = UrlConfig.muser_overcheck;
		CmwMcore.disabled('btn_finish');
		$.ajax(url, {
	        data : data,  
	        dataType : 'jsonp',  
	        crossDomain: true,  
	        success: function(data) {
	        		CmwMcore.enabled('btn_finish');
	        		data = JSON.parse(data);
	        		var success = data.success;
	        		if(!success){
	        			mui.alert(data.msg);
	        		}else{
	        			var actionType = plus.webview.currentWebview().actionType;
	             				MuiEventMgr.checkSecuity(function(){
								var webvewId = plus.webview.currentWebview().id;
								plus.storage.setItem("validRegisterId",webvewId);
							});
							$("#phone").text("");
			                $("#vcode").val("");
	        			
	        		}
	        },
	        error : function(req,textStatus, errorThrown){
	        		CmwMcore.enabled('btn_finish');
	        		mui.alert("验证码不对，请重新填写！");
	        }
	    });
	}

	function forward(){
		var key = KeyCfg.targetPageId;
		var targetPageId = plus.storage.getItem(key);
		if(!targetPageId){
		   
			mui.back();
		}else{
			var loginPage = plus.webview.getWebviewById(targetPageId);
			loginPage.show();
			mui.fire(loginPage,'reload',{});
			if(key) plus.storage.removeItem(key);
		}
		
	}
	
});

function sendSms(pars){
	$("#vcode").val("");
	var mask = mui.createMask(function(){
		
	});//callback为用户点击蒙版时自动执行的回调；
	mask.show();//显示遮罩
	var url = UrlConfig.msms_index;
	$.ajax(url, {
        data : pars,  
        dataType : 'jsonp',  
        crossDomain: true,  
        success: function(data) {
        		data = JSON.parse(data);
        		callback(data.msg);
        		mask.close();
        },
        error : function(req,textStatus, errorThrown){
        		mask.close();
        		mui.alert("验证码不对，请重新填写！");
        }
    });
}

function openNote(){
	mui.alert("                  注意以下事项\n1、根据省份、城市、地区不同,一般会在5秒-3分钟内收到验证码。\n2、如果您在验证过程中,出现任何问题,请致电400-812-2468。\n3、如果您想重新输入手机号码,请关闭app后，重新打开并注册。");
}

function callback(msg){
	if(msg==1){
		mui.alert("短信发送成功！");
		fun_timedownn(180);
		statusglobal3 = true;
	}else if(msg == 0){
		 $("#sendsms").html("重新获取");
		mui.alert("短信发送失败");
	}else if(msg==2){
		 $("#sendsms").html("重新获取");
		mui.alert("发送频率过高，2分钟之内不能重复发送");
	}else if(msg==3){
		 $("#sendsms").html("重新获取");
		mui.alert("没有要发送的号码");
	}else if(msg==4){
		 $("#sendsms").html("重新获取");
		mui.alert("您还没有登录呢，不能发送短信");
	}
}

function fun_timedownn(time)  {
    if(time=='undefined')
        time = 300;
    $("#sendsms").html(time+"秒");
    
    time = time-1;
    if(time>=0){
        setTimeout("fun_timedownn("+time+")",1000);
    }else {
    	 $("#sendsms").html("重新获取");
    }
}

var btnSendsms = CmwMcore.$('sendsms');
MeventMgr.onTouchstart(btnSendsms,function(event,self){
	 var text = $("#sendsms").text();
	 if(text != "重新获取") return;
	 sendSms(pars);
});
